﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace CreatePerformanceCounters
{
    public class Program
    {
        private static IPerformanceCounterController _helper;

        public static void Main(string[] args)
        {
            MainWithHelper(args, 
                new PerformanceCounterController());
        }

        internal static void MainWithHelper(string[] args, IPerformanceCounterController helper)
        {
            if (args == null || args.Length == 0)
            {
                DisplayHelp();
                return;
            }

            _helper = helper;
            string action = args[0];
            string json = (args.Length >= 2) ? args[1] : string.Empty;

            switch (action)
            {
                case "-v":
                case "-V":
                case "/v":
                case "/V":
                    ValidatePerformanceCounters(json);
                    break;
                case "-c":
                case "-C":
                case "/c":
                case "/C":
                    CreatePerformanceCounters(json);
                    break;
                case "-h":
                case "-H":
                case "/h":
                case "/H":
                case "-?":
                case "/?":
                    DisplayHelp();
                    break;
                default:
                    DisplayNotRecognizedParam();
                    DisplayHelp();
                    break;
            }
        }

        private static void DisplayNotRecognizedParam()
        {
            Console.Write(Messages.NotRecognizedParamMessage);
        }

        private static void DisplayHelp()
        {
            Console.Write(Messages.HelpMessage);
        }

        private static void CreatePerformanceCounters(string json)
        {
            try
            {
                _helper.Create(json);
                Console.Write(Messages.PerformanceCountersCreated);
            }
            catch (Exception ex)
            {
                Console.Write(Messages.PerformanceCountersNotCreated);
                Trace.Write(ex.ToString());
            }
        }

        private static void ValidatePerformanceCounters(string json)
        {
            try
            {
                _helper.Validate(json);
                Console.Write(Messages.ValidatedMessage);
            }
            catch (Exception ex)
            {
                Trace.Write(ex.ToString());
                Console.Write(Messages.NotValidatedMessage);
            }
        }
    }
}